<?php

require_once ROOT_DIR.'models/TodoList.php';

/**
 * Tries to modify the list stored under the given id and echos the result.
 * @param int $id
 * @param string $name
 * @param string $description
 */
function editList($id, $name, $description){
	$list = TodoList::getList($id);
	if(!is_null($list)){
		if($list->getOwnerID() == $_SESSION['USER_ID'] || $_SESSION['USER_NAME'] == 'admin'){
			if(!is_null($name)){
				$list->setName($name);
			}
			
			if(!is_null($description)){
				$list->setDescription($description);
			}
			
			if($list->saveToDB()){
				$res = new ResultJSON(false, session_id());
				$res->addItem('lID', $list->getID());
				echo $res->renderView();
			} else {
				$res = new ResultJSON(true, session_id(), ERROR_ACTION_FAILED);
				echo $res->renderView();
			}
		} else {
			$res = new ResultJSON(true, session_id(), ERROR_NOT_ALLOWED);
			echo $res->renderView();
		}
	} else {
		$res = new ResultJSON(true, session_id(), ERROR_LIST_DOES_NOT_EXIST);
		echo $res->renderView();
	}
}

?>